package Limit.Game.MarkSix;



import l1j.server.server.ClientThread;
import l1j.server.server.clientpackets.ClientBasePacket;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.serverpackets.S_NPCTalkReturn;
import l1j.server.server.serverpackets.S_SystemMessage;


public class E_MarkSix  extends ClientBasePacket {
	private static final String E_MarkSix = "[E] E_MarkSix";
	
	public E_MarkSix(byte abyte0[], ClientThread client) throws Exception 
	{
		super(abyte0);
		int objid = readD();
		String s = readS();
		
		L1PcInstance pc = client.getActiveChar();
		L1Object obj = L1World.getInstance().findObject(objid);
		
		String htmlid = "";
		String[] htmldata = null;
		
		
		
		if (((L1NpcInstance) obj).getNpcTemplate().get_npcId() == 901018)
		{
			if (obj != null) 
			{
				if (obj instanceof L1NpcInstance) 
				{
					L1NpcInstance npc = (L1NpcInstance) obj;
					int difflocx = Math.abs(pc.getX() - npc.getX());
					int difflocy = Math.abs(pc.getY() - npc.getY());
					
					if (!(obj instanceof L1PetInstance)
						&& !(obj instanceof L1SummonInstance)) 
					{					
						if ((difflocx > 3) || (difflocy > 3)) 
						{ // 3格以上的距離對話無效
							return;
							}
					}
					npc.onFinalAction(pc, s);
				}
			}
			if (((L1NpcInstance) obj).getNpcTemplate().get_npcId() == 901018)
			{
				
				
				String[] Data = new String[8];
				//重置六個號碼圖片為空白，3339為空白圖片編號
				for(int i = 1; i <= 6; i++)
					Data[i] = "3339";
				//加注码
				if (s.equalsIgnoreCase("marksix_plus_one") || s.equalsIgnoreCase("marksix_plus_ten"))
				{
					/**
					 * MarkSix_FP 六合彩第一頁選擇彩票類型
					 * MarkSix_SP 第二頁　大小單雙
					 * MarkSix_TP 第三頁　特碼二星。。。
					 */
					MarkSix_Tickets_Table_Temp mark = new MarkSix_Tickets_Table_Temp();
					mark.setCharid(pc.getId());
					mark.loadCurrentData();
					int warger = Integer.parseInt(mark.results[0]);
					if (s.equalsIgnoreCase("marksix_plus_one")){
						mark.setWager(warger + 10);
					}else{
						mark.setWager(warger + 100);
					}
					mark.saveMarkTicketsOfTemp();
					mark.loadCurrentData();
					
					if (Integer.parseInt(mark.results[7]) > 6){
						for(int i = 0; i <= 7; i++)
							Data[i] = mark.results[i];
						switch (Integer.parseInt(mark.results[7])){
						case 7:Data[1] = "1864";break;
						case 8:Data[1] = "1863";break;
						case 9:Data[1] = "1865";break;
						case 10:Data[1] = "1866";break;
						}
						htmlid = "MarkSix_SP";
					}else{						
						for(int i = 0; i <= Integer.parseInt(mark.results[7]); i++){
							if (mark.results[i].equalsIgnoreCase("*")){
								Data[i] = "3339";
							}else if ( i>0 && i<7){
								Data[i] = String.valueOf(1813 + Integer.parseInt(mark.results[i]));
							}else{
								Data[i] = mark.results[i];
							}
						}
						htmlid = "MarkSix_TP";
					} 
					htmldata = Data;
					/*} else {
					  pc.sendPackets(new S_SystemMessage("所需道具不足。"));
				    }*/				
				}else if (s.equalsIgnoreCase("marksix_check")){
					
					MarkSix_Data_Table data = new MarkSix_Data_Table();
					String[] nums = data.loadMarkSixInfo();
					for (int i = 0; i < nums.length; i++){
						if(Integer.parseInt(nums[i]) == 0)
							nums[i] = "1804";
						else 
							nums[i] = ""+(Integer.parseInt(nums[i])+1813);
					}
					htmldata = nums;
					htmlid = "MarkSix_CK";		
					
				}else if (s.equalsIgnoreCase("marksix_clean")){//清除重選
					
					MarkSix_Tickets_Table_Temp mark = new MarkSix_Tickets_Table_Temp();
					mark.setCharid(pc.getId());					
					mark.resetMarkTicketsNum();
					mark.loadCurrentData();
					
					for(int i = 0; i <= Integer.parseInt(mark.results[7]); i++){
						if (mark.results[i].equalsIgnoreCase("*")){
							Data[i] = "3339";
						}else if ( i>0 && i<7){
							Data[i] = String.valueOf(1813 + Integer.parseInt(mark.results[i]));
						}else{
							Data[i] = mark.results[i];
						}
					}
					htmlid = "MarkSix_TP";
					htmldata = Data;		
					
				}else if (s.equalsIgnoreCase("marksix_back")){//返回上一頁
					
					htmlid = "MarkSix_FP";
					MarkSix_Tickets_Table_Temp mark = new MarkSix_Tickets_Table_Temp();
					mark.setCharid(pc.getId());
					mark.delMarkTicketsNum();
					Data[0] = "0";
					htmldata = Data;
					
				}else if(s.equalsIgnoreCase("marksix_buy")){//確定購買
		
					String[] okNum = new String[6];
					int type = 0;
					MarkSix_Tickets_Table_Temp mark = new MarkSix_Tickets_Table_Temp();
					mark.setCharid(pc.getId());
					mark.loadCurrentData();
					for(int i = 1; i < 7; i++){//獲取當前已經選到第幾個號碼
						if (mark.results[i].equals("0") || mark.results[i].equals("*")){
							mark.setNumber(i-1);
							break;
						}else if (i == 6){
							mark.setNumber(i);
						}
					}
					type = Integer.parseInt(mark.results[7]);					
					if (mark.num < type && type < 7){ //檢測玩家是完成選號及注碼
						pc.sendPackets(new S_SystemMessage("系統提示：您還沒選完哦！"));  //同時禁止選大小單雙進入本檢測	
						for(int i = 0; i <= type; i++)
							Data[i] = mark.results[i]+",";
						htmlid = "MarkSix_TP";
					} else if (Integer.parseInt(mark.results[0]) == 0){
						pc.sendPackets(new S_SystemMessage("系統提示：您還沒有壓注哦！"));
						if (Integer.parseInt(mark.results[7]) < 7){
							for(int i = 0; i <= type; i++){
								if (mark.results[i].equalsIgnoreCase("*")){
									Data[i] = "3339";
								}else if ( i>0 && i<7){
									Data[i] = String.valueOf(1813 + Integer.parseInt(mark.results[i]));
								}else{
									Data[i] = mark.results[i];
								}
							}
							htmlid = "MarkSix_TP";
						}else{
							type = 6;
							for(int i = 0; i <= type; i++)
								Data[i] = mark.results[i];
							switch (Integer.parseInt(mark.results[7])){
							case 7:Data[1] = "1864";break;
							case 8:Data[1] = "1863";break;
							case 9:Data[1] = "1865";break;
							case 10:Data[1] = "1866";break;
							}
							htmlid = "MarkSix_SP";
						}						
					} 
					else if ((!pc.getInventory().checkItem(70207, Integer.parseInt(mark.results[0]))))
					{
						pc.sendPackets(new S_SystemMessage("系統提示：您的娛樂幣不足！"));
						if (Integer.parseInt(mark.results[7]) < 7){
							for(int i = 0; i <= type; i++){
								if (mark.results[i].equalsIgnoreCase("*")){
									Data[i] = "3339";
								}else if ( i>0 && i<7){
									Data[i] = String.valueOf(1813 + Integer.parseInt(mark.results[i]));
								}else{
									Data[i] = mark.results[i];
								}
							}
							htmlid = "MarkSix_TP";
						}else{
							type = 6;
							for(int i = 0; i <= type; i++)
								Data[i] = mark.results[i];
							switch (Integer.parseInt(mark.results[7])){
							case 7:Data[1] = "1864";break;
							case 8:Data[1] = "1863";break;
							case 9:Data[1] = "1865";break;
							case 10:Data[1] = "1866";break;
							}
							htmlid = "MarkSix_SP";
						}
					}
					else{
						mark.setCharid(pc.getId());
						mark.setType(Integer.parseInt(mark.results[7]));
						mark.setWager(Integer.parseInt(mark.results[0]));
						pc.getInventory().consumeItem(70207, Integer.parseInt(mark.results[0]));
						for (int i=1; i<7; i++){
							okNum[i-1] = mark.results[i];
						}
						mark.buyMarkTicketsNum(okNum);
						//寫入匯總表type+1 wager+
						MarkSix_Data_Table dataTotal = new MarkSix_Data_Table();
						
						int typeNum[] = new int[]{0,0,0,0,0,0,0,0,0,0,0};
						for (int i=1; i<=typeNum.length; i++){
							if (Integer.parseInt(mark.results[7]) == i){
								typeNum[i] = Integer.parseInt(mark.results[0]) / 10;
								break;
							}
						}
						dataTotal.updateMarkSixTotalTypeNum(typeNum);
						dataTotal.updateMarkSixTotalWager(Integer.parseInt(mark.results[0]));
						
						pc.sendPackets(new S_SystemMessage("系統提示：購買成功！"));
						mark.setCharid(pc.getId());
						mark.delMarkTicketsNum();
						htmlid = "MarkSix_FP";
					}					
					htmldata = Data;
					
				} else if (s.equalsIgnoreCase("MS_GM")) {
					MarkSix_StrikeTickets_Table strike = new MarkSix_StrikeTickets_Table();
					int count = strike.StrikeBonus(pc.getId());
					strike.deleteStrikeTickets(pc.getId());
					if ( count > 0)
					{
						L1ItemInstance item = pc.getInventory().storeItem(70207, count);
						pc.sendPackets(new S_SystemMessage("獲得" + item.getName() + count + "個。"));	
					}
					else
					{
						pc.sendPackets(new S_SystemMessage("您沒有已中獎的彩票。"));	
					}
				} else {	
					String[] whichs = s.split("_");
					int which = Integer.parseInt(whichs[1]);

					if (s.equalsIgnoreCase("type_" + which))//選彩票類型
					{
						if (MarkSix._canBuyTime)
						{
							MarkSix_Tickets_Table_Temp mark = new MarkSix_Tickets_Table_Temp();
							mark.setCharid(pc.getId());
							mark.setType(which);
							if (mark.countMarkTickets() > 0 ){
								mark.loadCurrentData();
							}else{
								mark.saveMarkTicketsOfTemp();
								mark.loadCurrentData();
							}
							
							if (which > 6)
							{
								switch (which){
								case 7:Data[1] = "1864";break;
								case 8:Data[1] = "1863";break;
								case 9:Data[1] = "1865";break;
								case 10:Data[1] = "1866";break;
								}
								Data[0] = mark.results[0];
								htmlid = "MarkSix_SP";
							}
							else
							{
								for(int i = 0; i <= which; i++){
									if (mark.results[i].equalsIgnoreCase("*")){
										Data[i] = "3339";
									}else if ( i>0 && i<7){
										Data[i] = String.valueOf(1813 + Integer.parseInt(mark.results[i]));
									}else{
										Data[i] = mark.results[i];
									}
								}
								htmlid = "MarkSix_TP";
							}
							htmldata = Data;
						} else {
							htmlid = "MarkSix_S";
						}
					}
					else if (s.equalsIgnoreCase("num_" + which))//選號碼  which 當前的號碼
					{				
						//if(pc.getInventory().checkItem(70117, 1)){
						
						MarkSix_Tickets_Table_Temp mark = new MarkSix_Tickets_Table_Temp();
						mark.setCharid(pc.getId());
						mark.setTicketsNum(which);
						mark.loadCurrentData();
						for(int i = 1; i <= 6; i++){//獲取當前已經選到第幾個號碼
							if (mark.results[i].equals("0") || mark.results[i].equals("*")){
								mark.setNumber(i);
								break;
							}
						}
						int checkNum = 0;
						for (int i=1; i<mark.num; i++){
							if (mark.ticketsNum == Integer.parseInt(mark.results[i])){
								checkNum = 1;
								pc.sendPackets(new S_SystemMessage("系統提示：不可選重複的號碼！"));
							}
						}
						if (checkNum == 0 && mark.num <= Integer.parseInt(mark.results[7])){
							mark.saveMarkTicketsOfTemp();
							mark.loadCurrentData();
						}
						for(int i = 0; i <= Integer.parseInt(mark.results[7]); i++){
							if (mark.results[i].equalsIgnoreCase("*")){
								Data[i] = "3339";
							}else if ( i>0 && i<7){
								Data[i] = String.valueOf(1813 + Integer.parseInt(mark.results[i]));
							}else{
								Data[i] = mark.results[i];
							}
						} 
						htmlid = "MarkSix_TP";
						htmldata = Data;
						/*} else {
							pc.sendPackets(new S_SystemMessage("所需道具不足。"));
						}*/

					}
					
				}
				
				
					
				if (htmlid != null) 
				{
					pc.sendPackets(new S_NPCTalkReturn(objid, htmlid, htmldata));
				}
			}
		}
	}

	public String getType() 
	{
		return E_MarkSix;
	}
}
